﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.Model;
using WpfSnqkGasAnalysis.IBusiness_impl;
using System.Text.RegularExpressions;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 净化气。审核窗口
    /// </summary>
    public partial class WindowCleanCheck : Window
    {
        public WindowCleanCheck()
        {
            InitializeComponent();
        }
        /// <summary>
         /// 选中的数据
         /// </summary>
        internal Clean_gas clean_Gas { get; set; }

        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();
        CleanGasBusiness cleanGasBusiness = new CleanGasBusiness();
        UserBusiness userBusiness = new UserBusiness();

        //驳回  //创建时间：2022-12-14 22:51:10。
        private void reject_Click(object sender, RoutedEventArgs e)
        {
            if (this.txtRejectReason.Text.Equals("请输入驳回理由") || string.IsNullOrWhiteSpace(txtRejectReason.Text))
            {
                ShowAlertHelpter.ShowAlert("请输入驳回理由");
                e.Handled = true;
                return;
            }
            Clean_gas_check_history history = new Clean_gas_check_history()
            {
                Jing_hua_qi_key = clean_Gas.Id,
                Reject_reason= txtRejectReason.Text.Trim(),
                Check_stat = 0,
            };
            var result = cleanGasBusiness.Check(history);
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //审核通过    //创建时间：2022-12-14 22:55:22
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            Clean_gas_check_history history = new Clean_gas_check_history() { 
                Jing_hua_qi_key= clean_Gas.Id,
                Check_stat=1,
            };
            var result = cleanGasBusiness.Check(history);
            if (result.CodeEnum==ResultCode.Success)
            {
                btnConfirm.IsEnabled = false;
                btnReject.IsEnabled = false;

                //审核通过的，生成pdf文件  //创建时间：2023-1-12 09:52:05。作者：王浩力
                //查询完整数据      
                var model = cleanGasBusiness.GetCmboxStr(clean_Gas.Id);
                string pdfFileName = $"净化气{clean_Gas.Sample_no}井{clean_Gas.Well_name}";
                MakePdfHelpter.CheckPassLoadXps(pdfFileName, "FlowDocumentCleanPrint.xaml", model, 
                    () => {
                    string dir = DateTime.Now.ToString("yyyy");
                    dir = System.IO.Path.Combine("净化气", dir);
                    return dir;
                });
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //退出     //创建时间：2022-12-14 22:54:44。
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var all = sourceDictBusiness.GetAll();
            all = all.Where(g => g.D_stat == 1).ToList();

            //取样条件
            cmb_sampling_condiction.ItemsSource = all.Where(g => g.Group_id.Equals("quyangCondition")).ToList();
            cmb_sampling_condiction.DisplayMemberPath = "Item_name";
            cmb_sampling_condiction.SelectedValuePath = "Id";
            cmb_sampling_condiction.SelectedValue = clean_Gas.Sampling_condiction;

            //分析单位
            cmb_analysis_company.ItemsSource = all.Where(g => g.Group_id.Equals("quyang")).ToList();
            cmb_analysis_company.DisplayMemberPath = "Item_name";
            cmb_analysis_company.SelectedValuePath = "Id";
            cmb_analysis_company.SelectedValue = clean_Gas.Analysis_company;

            //原取样位置
            original_sampling_position.ItemsSource = all.Where(g => g.Group_id.Equals("quyangLocation")).ToList();
            original_sampling_position.DisplayMemberPath = "Item_name";
            original_sampling_position.SelectedValuePath = "Id";
            //original_sampling_position.SelectedValue = clean_Gas.Original_sampling_position;

            //创建时间：2023-1-15 16:57:39。
            if (Regex.IsMatch(clean_Gas.Original_sampling_position, "^\\d{19}"))
            {
                original_sampling_position.SelectedValue = clean_Gas.Original_sampling_position;
            }
            else
            {
                txt_sampling_position2.Text = clean_Gas.Original_sampling_position;
            }

            var userList = userBusiness.GetAllUser();
            userList = userList.FindAll(g => g.U_stat == 1);

            //取样人
            cmb_sampling_user.ItemsSource = userList;
            cmb_sampling_user.DisplayMemberPath = "User_real_name";
            cmb_sampling_user.SelectedValuePath = "Id";
            cmb_sampling_user.SelectedValue = clean_Gas.Sampling_user;

            //取样地点
            sampling_location.ItemsSource = all.Where(g => g.Group_id.Equals("quyangAddress")).ToList();
            sampling_location.DisplayMemberPath = "Item_name";
            sampling_location.SelectedValuePath = "Id";
            sampling_location.SelectedValue = clean_Gas.Sampling_location;

            //净取样位置
            net_sampling_position.ItemsSource = all.Where(g => g.Group_id.Equals("quyangLocation")).ToList();
            net_sampling_position.DisplayMemberPath = "Item_name";
            net_sampling_position.SelectedValuePath = "Id";
            //net_sampling_position.SelectedValue = clean_Gas.Net_sampling_position;

            //创建时间：2023-1-15 17:09:14
            if (Regex.IsMatch(clean_Gas.Net_sampling_position, "^\\d{19}"))
            {
                net_sampling_position.SelectedValue = clean_Gas.Net_sampling_position;
            }
            else
            {
                txt_net_sampling_position2.Text = clean_Gas.Net_sampling_position;
            }

            //分析人
            cmb_analysis_user.ItemsSource = userList;
            cmb_analysis_user.DisplayMemberPath = "User_real_name";
            cmb_analysis_user.SelectedValuePath = "Id";
            cmb_analysis_user.SelectedValue = clean_Gas.Analysis_user;

            //编辑数据载入  //2022-12-14 14:57:25
            txtYpbh.Text = clean_Gas.Sample_no;
            txt_air_source.Text = clean_Gas.Air_source;
            txt_sampling_temperature.Text = clean_Gas.Sampling_temperature.ToString();

            well_name.Text = clean_Gas.Well_name;
            if (DateTime.TryParse(clean_Gas.Analysis_time, out DateTime analysTime))
            {
                date_analysis_time.SelectedDateTime = analysTime;
            }
            if (DateTime.TryParse(clean_Gas.Sampling_time, out DateTime sampling_time))
            {
                date_sampling_time.SelectedDateTime = sampling_time;

                //移除输入焦点
                date_sampling_time.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            }

            txt_feedgas_percent.Text = clean_Gas.Feedgas_percent.ToString();
            txt_feedgas_mg_per_m3.Text = clean_Gas.Feedgas_mg_per_m3.ToString();
            txt_clean_gas_percent.Text = clean_Gas.Clean_gas_percent.ToString();
            txt_clean_gas_mg_per_m3.Text = clean_Gas.Clean_gas_mg_per_m3.ToString();

            //设置所有输入控件只读
            #region 设置所有输入控件只读
            var grid = (DependencyObject)this.Content;
            List<DependencyObject> list = new List<DependencyObject>();
            Action<DependencyObject> action = null;
            action = (a) =>
            {
                var count = VisualTreeHelper.GetChildrenCount(a);
                for (int i = 0; i < count; i++)
                {
                    var elem = VisualTreeHelper.GetChild(a, i);
                    list.Add(elem);
                    action(elem);
                }
            };

            action(grid);

            foreach (var item in list)
            {
                if (item is TextBox)
                {
                    var elem = (item as TextBox);
                    if (elem.Name.Equals("txtRejectReason"))
                    {
                        continue;
                    }
                    (item as TextBox).IsEnabled = false; //批量设置输入框禁用
                }
                else if (item is ComboBox)
                {
                    (item as ComboBox).IsEnabled = false;
                }
            }

            #endregion

        }

        private void txtRejectReason_GotFocus(object sender, RoutedEventArgs e)
        {
            this.txtRejectReason.Text = "";
        }

        //打印预览
        private void print_Click(object sender, RoutedEventArgs e)
        {             
            //查询完整数据
            CleanGasBusiness busi = new CleanGasBusiness();
            var model = busi.GetCmboxStr(clean_Gas.Id);

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentCleanPrint.xaml", model, new PrintGas_analysisDocument());
            //ppv.Owner = this.Content;
            ppv.ShowInTaskbar = false;
            ppv.Show();
        }
    }
}
